package com.synjones.ui.recognize;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.synjones.AppExecutors;
import com.synjones.entry.SignPerson;
import com.synjones.face.camera.FaceHelper;
import com.synjones.face.camera.FaceRectView;
import com.synjones.idcardusb_reader.R;
import com.synjones.ui.FirstScreenRouter;
import com.synjones.ui.SecondScreenRouter;

import java.util.List;

public class RecognizeSecondScreen extends FrameLayout implements RecognizeContract.View {

    private TextView recognizeName;
    private TextView recognizeSex;
    private TextView recognizeWork;
    private TextView recognizeHotel;
    private TextView recognizeRoom;
    private TextView recognizeScore;
    private ImageView recognizeCardFace;
    private ImageView recognizeFrame;
    private ImageView recognizeIdCard;
    private Button recognizeCancel;

    private TextureView cameraPreview;
    private FaceRectView faceRect;
    private FaceHelper faceHelper;

    private FirstScreenRouter router1;
    private SecondScreenRouter router2;

    private RecognizeContract.Presenter presenter;


    public RecognizeSecondScreen(@NonNull Context context,FirstScreenRouter firstScreenRouter,
                                 SecondScreenRouter secondScreenRouter) {
        super(context);
        router1 = firstScreenRouter;
        router2 = secondScreenRouter;
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_recongnize,null);
        initView(view);
        addView(view,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    public void  init(){
        presenter = new RecognizeSecPresenter(this,getContext(),faceHelper);
        faceHelper = new FaceHelper(cameraPreview, faceRect, getContext(), (Activity) router1);
        presenter.start();
    }


    private void initView(View view){
        recognizeName = (TextView) view.findViewById(R.id.recognizeName);
        recognizeSex = (TextView) view.findViewById(R.id.recognizeSex);
        recognizeScore = (TextView) view.findViewById(R.id.recognizeScore);
        recognizeWork = (TextView) view.findViewById(R.id.recognizeWork);
        recognizeHotel = (TextView) view.findViewById(R.id.recognizeHotel);
        recognizeRoom = (TextView) view.findViewById(R.id.recognizeRoom);
        recognizeCardFace = (ImageView) view.findViewById(R.id.recognizeCardFace);
        recognizeFrame = (ImageView) view.findViewById(R.id.recognizeFrame);
        recognizeIdCard = (ImageView) view.findViewById(R.id.recognizeIdCard);
        cameraPreview = (TextureView) view.findViewById(R.id.cameraPreview);
        faceRect = (FaceRectView) view.findViewById(R.id.faceRect);
        recognizeCancel = (Button) view.findViewById(R.id.recognizeCancel);
        recognizeCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                router2.gotoWelcome();
                router1.gotoWelcome();
            }
        });
    }

    @Override
    public void showCardInfo(final String name, final String sex, final String work,
                             final String hotel, final String room, final Bitmap cardFace) {
        AppExecutors.mainThread.execute(new Runnable() {
            @Override
            public void run() {
                recognizeName.setText(name);
                recognizeSex.setText(sex);
                recognizeWork.setText(work);
                recognizeHotel.setText(hotel);
                recognizeRoom.setText(room);
                recognizeCardFace.setImageBitmap(cardFace);
            }
        });
    }

    @Override
    public void showCompareInfo(final Bitmap frameFace, final Bitmap cardFace) {
        AppExecutors.mainThread.execute(new Runnable() {
            @Override
            public void run() {
                recognizeFrame.setImageBitmap(frameFace);
                recognizeIdCard.setImageBitmap(cardFace);
                if (frameFace!=null){
                    recognizeScore.setTextColor(Color.GREEN);
                    recognizeScore.setText("匹配");
                }else{
                    recognizeScore.setTextColor(Color.RED);
                    recognizeScore.setText("不匹配");
                }
            }
        });
    }

    @Override
    public void resetUI() {
        AppExecutors.mainThread.execute(new Runnable() {
            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void run() {
                recognizeName.setText("");
                recognizeSex.setText("");
                recognizeWork.setText("");
                recognizeHotel.setText("");
                recognizeRoom.setText("");
                recognizeScore.setText("对比结果");
                recognizeScore.setTextColor(getResources().getColor(R.color.infoTextColor,null));
                recognizeCardFace.setImageBitmap(null);
                recognizeFrame.setImageBitmap(null);
                recognizeIdCard.setImageBitmap(null);
            }
        });
    }

    @Override
    public void showBackInfo(String msg, int color) {
        router1.showToast(msg, color);
        router2.showToast(msg, color);
    }

    @Override
    public void isSelfCheckIn(boolean selfCheckIn) {
        presenter.setSelfCheckIn(selfCheckIn);
    }

    @Override
    public void goCheckPage(SignPerson signPerson) {
        router2.gotoCheck(signPerson);
    }

    @Override
    public void goOtherPage(int doWhat, List<SignPerson> data, String idno, String idname) {
        router1.gotoOthers(doWhat, data, idno, idname);
    }
    @Override
    public void pauseScan() {
        presenter.pauseScan();
    }

    @Override
    public void resumeScan() {
        presenter.resumeScan();
    }

    public void release() {
        presenter.release();
    }

    public void setSelfSign(boolean selfSign) {
        presenter.setSelfCheckIn(selfSign);
    }


}
